<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//cfg.php中include dbconnection.cfg
class baseinfo_common {

    //打开连接
    function OpenConnection() {
        $db = new DBConnection();
        $db->getConnection();
    }

    //取得指定表的总记录数，方便计算分页
    function getTableTotalRecords($tableName,$where="") {
        if($where=="")
        {
            $strSql = "select count(1) as totalRecors from `" . $tableName . "` where delete_flag=0";
        }
        else
        {
            $strSql = "select count(1) as totalRecors from `" . $tableName . "` where delete_flag=0 and (".$where.")";
        }   
        $handle = $this->execSql($strSql);
        $row = mysql_fetch_object($handle);
        return $row->totalRecors;
    }

    //取得分页数,用于单表查询
    function getTablePages($tableName, $pageSize,$where="") {
        if (!is_numeric($pageSize))
            $pageSize = 10; //default
        $tolNum = $this->getTableTotalRecords($tableName,$where);
        $tolPage=ceil($tolNum / $pageSize);
        return $tolPage;
    }

    //返回查询结果的句柄,用于复杂查询
    function getPageRecords($tableName, $orderByKey, $pageSize, $pageNo,$where="") {
        if (!is_numeric($pageSize))
            $pageSize = 10; //default
        if ($pageNo < 1)
            $pageNo = 1;

        $pagePos = ($pageNo - 1) * $pageSize;
        if($where=="")
        {
             $strSql = "select * from `" . $tableName . "` where delete_flag=0 ORDER BY " . $orderByKey . " LIMIT " . $pagePos . ", " . $pageSize . ";";
        }
        else
        {
             $strSql = "select * from `" . $tableName . "` where delete_flag=0 and (".$where.") ORDER BY " . $orderByKey . " LIMIT " . $pagePos . ", " . $pageSize . ";";
        }
        return $this->execSql($strSql);
    }
     //返回查询结果的句柄
    function getSqlPageRecords($strSelectOrderBy,  $pageSize, $pageNo) {
        if (!is_numeric($pageSize))
            $pageSize = 10; //default
        if ($pageNo < 1)
            $pageNo = 1;

        $pagePos = ($pageNo - 1) * $pageSize;
        
        $strSql = $strSelectOrderBy. " LIMIT " . $pagePos . ", " . $pageSize . ";";
        
        return $this->execSql($strSql);
    }


    //刪除記錄
    function deleteRecord($tableName, $keyColumnName, $keyValue) {
        //$strSql = "DELETE FROM `" . $tableName . "` WHERE `".$keyColumnName."`=" . $keyValue . ";";
        $strSql = "UPDATE `" . $tableName . "` SET delete_flag=1 WHERE `".$keyColumnName."`=" . $keyValue . ";";
        return $this->execSql($strSql);
    }
    //檢查是否已經存在此主鍵,用於是否可以更新或新增此值
    function checkExistKeyValue($tableName,$Value,$keyColumn="",$keyValue="",$keyColumnName="name_zh_hk")
    {
        if($keyValue=="" && $keyColumn=="")
        {
            $strSql="SELECT COUNT(1) AS existKey FROM ".$tableName." WHERE ".$keyColumnName." ='".$Value."' AND delete_flag=0";
        }
        else
        {
            $strSql="SELECT COUNT(1) AS existKey FROM ".$tableName." WHERE ".$keyColumnName." ='".$Value."' AND delete_flag=0 AND ".$keyColumn." !=".$keyValue;
        }
        $handle=$this->execSql($strSql);
        if($handle)
        {
            $row = mysql_fetch_object($handle);
            $existkey=$row->existKey;
            if(intval($existkey)==1)
            {
                return true;//存在
            }
            else
            {
                return false;//不存在 
            }
        }
        else
        {
            return "Sql error";
        }
    }
    function execSql($strSql) {
        $this->OpenConnection();
        $handle = mysql_query($strSql);
        return $handle;
    }
	
	//生成Mysql方式的去utf8空格串
	function cutf8trim($str)
	{
	   return "TRIM(REPLACE('".$str."',' ',' '))";
	}

	//在php中去除utf8串空格返回
	function utf8trim($str)
	{
	  $nstr=preg_replace('/[(\xc2\xa0)]+/', ' ', $str); 
	   return trim($nstr);
	}

}

?>
